import { useStableDiffusionStore } from "@/app/store/stableDiffusion";
import Http from "../http";
import { hash } from "ohash";
interface stableDiffusionModel {
  config: any;
  filename: string;
  hash: string;
  model_name: string;
  sha256: string;
  title: string;
}
interface Model {
  id: string;
  owned_by?: string;
  type: "image";
  source: string;
}
interface Txt2Img {
  prompt: string;
  negative_prompt: string;
  styles: string[] | null;
  seed: number;
  subseed: number;
  subseed_strength: number;
  seed_resize_from_h: number;
  seed_resize_from_w: number;
  sampler_name: string;
  batch_size: number;
  n_iter: number;
  steps: number;
  cfg_scale: number;
  width: number;
  height: number;
  restore_faces: boolean;
  tiling: boolean;
  do_not_save_samples: boolean;
  do_not_save_grid: boolean;
  eta: number | null;
  denoising_strength: number | null;
  s_min_uncond: number | null;
  s_churn: number | null;
  s_tmax: number | null;
  s_tmin: number | null;
  s_noise: number | null;
  override_settings: Record<string, any> | null;
  override_settings_restore_afterwards: boolean;
  refiner_checkpoint: string | null;
  refiner_switch_at: number | null;
  disable_extra_networks: boolean;
  comments: Record<string, any> | null;
  enable_hr: boolean;
  firstphase_width: number;
  firstphase_height: number;
  hr_scale: number;
  hr_upscaler: string | null;
  hr_second_pass_steps: number;
  hr_resize_x: number;
  hr_resize_y: number;
  hr_checkpoint_name: string | null;
  hr_sampler_name: string | null;
  hr_prompt: string;
  hr_negative_prompt: string;
  sampler_index: string;
  script_name: string | null;
  script_args: any[];
  send_images: boolean;
  save_images: boolean;
  alwayson_scripts: Record<string, any>;
}
interface SdImageRes {
  images: string[];
  info: string;
  parameters: Txt2Img;
}
const { url } = useStableDiffusionStore.getState();
export const SDApi = new (class stableDiffusionApi extends Http {
  getModels = async () => {
    const response: stableDiffusionModel[] = await this.get("", {
      allUrl: `${url}/sdapi/v1/sd-models/`,
    });
    const Models: Partial<Model>[] = response.map((e) => {
      return {
        id: e.title,
        owned_by: "stableDiffusion",
        source: "local",
        type: "image",
      };
    });
    return Models;
  };
  checkModel = async (ModelName: string) => {
    const response = await this.post("", {
      body: {
        data: [ModelName],
        fn_index: 1183,
        session_hash: hash(Date.now()),
      },
      allUrl: `${url}/run/predict/`,
    });
    return response;
  };
  generate = async (prompt = "粉红色长发的女孩"): Promise<SdImageRes> => {
    const { SDParams, defaultPrompt } = useStableDiffusionStore.getState();
    const params = eval("(" + SDParams + ")");
    let res;
    try {
      res = await this.post("", {
        body: {
          ...params,
          prompt: prompt + defaultPrompt ? defaultPrompt : "",
        },
        allUrl: `${url}/sdapi/v1/txt2img`,
      });
      return res;
    } catch (error) {
      console.log(error);
    }
    return res;
  };
  confirm = async () => {
    return await this.get(undefined, {
      allUrl: `${url}/app_id`,
    });
  };
  progress = async (isSkip: boolean = false) => {
    return await this.get(undefined, {
      allUrl: `${url}/sdapi/v1/progress?skip_current_image=${isSkip}`,
    });
  };
  exit = async () => {
    return await this.get(undefined, {
      allUrl: `${url}/sdapi/v1/exit`,
    });
  };
})();
